package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.SkillsInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;


/**
 * 
 * 个人中心技能
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class ModelCenterActivity extends RequestActivity {
	protected Context context = ModelCenterActivity.this;
	LinearLayout linlayer, linlayer1;
	public static List<SkillsInfoBean> xuanzhongModel;// 选中技能
	public List<SkillsInfoBean> listmodel;
	List<SkillsInfoBean> retList;

	TextView next;
	ImageView back;

	SkillsInfoBean rm;
	TextView tv;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		listmodel = new ArrayList<SkillsInfoBean>();
		xuanzhongModel = new ArrayList<SkillsInfoBean>();

		launchRequest(WooRequestFactory.getSkills());
	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		next = (TextView) this.findViewById(R.id.next);
		next.setOnClickListener(this);
		linlayer = (LinearLayout) this.findViewById(R.id.linlayer);
		linlayer1 = (LinearLayout) this.findViewById(R.id.linlayer1);
		addView(linlayer, listmodel, true);
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_SKILLSSUCCESS) == 1) {
			retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_SKILLS_DATA);
			selectData_info(listmodel, "t_dictionary", "pid=126");
			initView();
			linlayer1.removeAllViews();
			addView(linlayer1, xuanzhongModel, false);
		}

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.modelcenter_layout;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.next:
			launchRequest(WooRequestFactory.updateSkills());
			Utils.messageInfo.userInfo.setUserSkill(xuanzhongModel);
			String name = "";
			if (xuanzhongModel.size() > 1) {
				name = xuanzhongModel.get(0).getName() + "等"
						+ xuanzhongModel.size() + "个";
			} else if (xuanzhongModel.size() == 1) {
				name = xuanzhongModel.get(0).getName();
			}
			Intent intent = new Intent();
			System.out.println(xuanzhongModel.size() + "name:" + name);
			intent.putExtra("name", name);
			setResult(97, intent);
			finish();
			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

	private void addView(LinearLayout layer, List<SkillsInfoBean> listrim,
			final boolean flag) {
		List<View> listtextview = new ArrayList<View>();

		for (int i = 0; i < listrim.size(); i++) {
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.textviewitem1, null);
			final RelativeLayout rlayout = (RelativeLayout) view
					.findViewById(R.id.rlayout);
			int pasize = 0;
			ImageView papic = (ImageView) view.findViewById(R.id.papic);
			System.out.println("listrim.get(i).getIsPa():"
					+ listrim.get(i).getIsPa());
			if (!flag && listrim.get(i).getIsPa().equals("1")) {
				papic.setVisibility(View.VISIBLE);
				pasize = 16 + 5;
			}

			String text = listrim.get(i).getName();
			int level = listrim.get(i).getLevel();
			if (!flag) {
				switch (level) {
				case 13217:
					text += ":初级";
					papic.setBackgroundResource(R.drawable.pa_1);
					rlayout.setBackgroundResource(R.drawable.pa_chuji);
					break;
				case 13218:
					text += ":中级";
					papic.setBackgroundResource(R.drawable.pa_2);
					rlayout.setBackgroundResource(R.drawable.pa_zhongji);
					break;
				case 13219:
					text += ":高级";
					papic.setBackgroundResource(R.drawable.pa_3);
					rlayout.setBackgroundResource(R.drawable.pa_gaoji);
					break;
				}
			} else {
				rlayout.setBackgroundResource(R.drawable.weixuanzhong);
			}
			TextView textview = (TextView) view.findViewById(R.id.textview);
			textview.setText(text);
			textview.setTag(listrim.get(i));
			textview.setTextColor(Color.rgb(66, 66, 66));

			if (flag && listrim.get(i).getIsselected() == 0) {
				textview.setTextColor(Color.rgb(66, 66, 66));
				rlayout.setBackgroundResource(R.drawable.weixuanzhong);
			} else if (flag && listrim.get(i).getIsselected() == 1) {
				textview.setTextColor(Color.rgb(255, 255, 255));
				rlayout.setBackgroundResource(R.drawable.xuanzhong);
			}
			textview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tv = (TextView) v;
					rm = (SkillsInfoBean) tv.getTag();
					if (flag) {
						if (rm.getIsselected() == 0) {
							rm.setIsselected(1);
							tv.setTextColor(Color.rgb(255, 255, 255));
							rlayout.setBackgroundResource(R.drawable.xuanzhong);

							Intent intent = new Intent(
									ModelCenterActivity.this,
									ModelDialogActivity.class);
							startActivityForResult(intent, 0);

						} else {
							if (xuanzhongModel.contains(rm))
								xuanzhongModel.remove(rm);
							rm.setIsselected(0);
							linlayer1.removeAllViews();
							addView(linlayer1, xuanzhongModel, false);
							tv.setTextColor(Color.rgb(66, 66, 66));
							rlayout.setBackgroundResource(R.drawable.weixuanzhong);
						}
					}
				}
			});

			Paint paint = new Paint();
			paint.setTextSize(textview.getTextSize());
			float size = paint.measureText(textview.getText().toString());
			view.setTag((int) size + Utils.dip2px(this, 10 + 25 + pasize));

			listtextview.add(view);
		}
		int j = 0;
		LinearLayout llayout = null;
		int MaxWidth = MainActivity.widthPixels - Utils.dip2px(this, 20);
		int temporarywidth = 0;
		while (true) {
			View view = null;
			int tagwidth = 0;
			if (j == listtextview.size()) {
				return;
			} else {
				view = listtextview.get(j);
				tagwidth = (Integer) view.getTag();
			}
			if (j == 0) {
				llayout = new LinearLayout(this);
			}
			System.out.println("textview:" + tagwidth + "temporarywidth:"
					+ temporarywidth + "MaxWidth:" + MaxWidth);
			if (tagwidth + temporarywidth < MaxWidth) {
				temporarywidth += tagwidth;
				llayout.addView(view);
			} else {
				layer.addView(llayout);

				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(0, 15, 0, 0);

				llayout = new LinearLayout(this);
				llayout.setLayoutParams(layoutParams);
				temporarywidth = (Integer) view.getTag();

				llayout.addView(view);
			}

			if (j == listtextview.size() - 1) {
				layer.addView(llayout);
			}
			j++;
		}
	}

	private void selectData_info(List<SkillsInfoBean> listModel,
			String tabname, String selection) {

		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				null);
		while (cursor.moveToNext()) {
			SkillsInfoBean skill = new SkillsInfoBean();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name_cn"));
			skill.setSkillID(id);
			skill.setName(name);
			skill.setIsselected(0);
			if (retList != null) {
				for (int i = 0; i < retList.size(); i++) {
					if (id == retList.get(i).getSkillID()) {
						skill.setIsselected(1);
						skill.setLevel(retList.get(i).getLevel());
						skill.setIsPa(retList.get(i).getIsPa());
						xuanzhongModel.add(skill);
					}
				}
			}
			listModel.add(skill);
		}
		cursor.close();
		db.close();
		dh.close();

	}

	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {

		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			boolean ispa = intent.getBooleanExtra("ispa", false);
			int level = intent.getIntExtra("level", 13217);
			String ispastr = "0";
			rm.setLevel(level);
			if (ispa)
				ispastr = "1";
			rm.setIsPa(ispastr);
			xuanzhongModel.add(rm);
			linlayer1.removeAllViews();
			addView(linlayer1, xuanzhongModel, false);
			break;
		case 2:
			rm.setIsselected(0);
			tv.setTextColor(Color.rgb(66, 66, 66));
			tv.setBackgroundResource(R.drawable.weixuanzhong);
			break;
		default:
			break;
		}
	}
}
